Skip to content

os/armino,os/board/bk7239n: Fix hang and crash paths found by static analysis in bk7239n PM, RTC and I2C - #7626

Open
Poly-J wants to merge 2 commits into
Samsung:masterfrom
bekencorp:platform_fix_code_dev
Open

Poly-J wants to merge 2 commits into
Samsung:masterfrom
bekencorp:platform_fix_code_dev

Conversation

@Poly-J

@Poly-J Poly-J commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Static analysis of the kernel reported hang and crash findings in the
BK7239n board support code and the ARMINO architecture layer. This PR
fixes the findings where an abnormal input, a resource failure or a
stalled clock turned a recoverable error into an infinite wait or a NULL
pointer access.

Changes

  1. os/board/bk7239n

    • bk_pm: do not create the PM worker when its queue cannot be
      created, release the queue if thread creation fails, back off on a
      pop error instead of spinning, and keep the thread alive when alarm
      registration fails.
    • RTC: bound the 32K tick reset wait in ana_rtc_hw_init() and skip it
      when the base tick is 0. Previously this could hang the boot.
  2. os/arch/arm/src/armino

    • RTC lower half: return -EINVAL for NULL lower/rtctime in
      armino_rdtime() and armino_settime(). Both are reachable through
      RTC_RD_TIME / RTC_SET_TIME with a NULL ioctl argument.
    • I2C: return ERROR when dev or msgs is NULL in armino_i2c_transfer(),
      matching the checks already done by setaddress() and setclock().

* already check dev the same way.
*/
if (priv == NULL || msgs == NULL) {
i2cinfo("i2c dev/msgs is NULL\n");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We can use error or warning in this case instead of info. @eunwoo-nam Could you guide him which one is proper?
  2. We can find the reason why this function returns ERROR by null priv or by null msgs. Let's print the value of both for easiler debugging.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunghan-chang @eunwoo-nam
I'd prefer i2cerr : a NULL dev / msgs is caller misuse and we return ERROR , so error level fits. But this file defines i2cinfo as an empty macro, and i2cerr also requires CONFIG_DEBUG_I2C_ERROR to print, so neither shows by default. Would lowsyslog(LOG_ERR, ...) be acceptable instead?

Comment on lines 211 to +212
DEBUGASSERT(lower != NULL && rtctime != NULL);
if (lower == NULL || rtctime == NULL) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eunwoo-nam Do we need to keep both line 211 and line 212?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two checks are redundant, and with assertions enabled DEBUGASSERT panics before reaching -EINVAL . armino_settime() (line 255) uses only the if check. Can we drop DEBUGASSERT and keep just the if ?

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against the merge-base (278667a9; the branch is 35 commits behind master, but the four files are self-contained). Not an approval — that's for a maintainer.

Went through all four files. Everything checks out; the RTC boot hang is the headline and it's a real one.

RTC: the boot hang is live, not theoretical

On master, ana_rtc_hw_init() is:

while (1) {
    if (aon_pmu_hal_rtc_tick_l_get() < s_ana_rtc_base_tick)
        break;
}

aon_pmu_hal_rtc_tick_l_get() returns uint32_t and s_ana_rtc_base_tick is set at l.880 from the same call just above — so if the 32K clock hasn't ticked yet and the hardware reads 0, base == 0 and tick < 0 is never true for unsigned. That's an unbounded loop on the boot path. Your two-part fix is right:

  • base == 0 → skip the wait entirely, with a warning. Correct: there's nothing to wait for if the base is zero.
  • Otherwise, bound by iteration count (ANA_RTC_TICK_RESET_RETRY_MAX = 100000), not by a tick-based timeout. Your comment explains why that's the right choice — a stalled 32K clock would freeze any tick-derived deadline, so only a loop count is meaningful here. Good reasoning; that's the detail a naive fix would get wrong.

PM thread (bk_pm_thread.c) — four correct changes

  • return BK_FAIL → break on register rxd_wk_alarm failure: it's inside the message switch, so break exits the switch and the loop keeps running. ret is already != BK_OK from the check, so the failure is still reported upward — the thread just doesn't die. Right.
  • else { rtos_delay_milliseconds(10); } on pop failure: on master a failed rtos_pop_from_queue fell straight back into the loop with no delay, i.e. a busy-spin at thread priority. The backoff fixes that.
  • Re-init guard if (s_queue != NULL) return BK_OK prevents a second pm_thread_main() from leaking a queue.
  • Thread-create failure now rtos_deinit_queue(&s_queue) and NULLs it — on master the queue leaked.

I checked the asymmetry on the two failure paths, and it's correct

Queue-create failure does s_queue = NULL with no deinit; thread-create failure does call deinit. That looks inconsistent but it's right: rtos_init_queue (rtos_pub.c:443) does mq_open first and returns kGeneralErr on failure before any kmm_malloc, so there's nothing to tear down. And rtos_deinit_queue (rtos_pub.c:548) early-returns on *queue == NULL, so it's safe where you do call it.

armino_i2c.c / armino_rtc_lowerhalf.c

Both match the description. I2C_TRANSFER() has no upstream check so the callee validating priv/msgs is right, and setaddress()/setclock() already do the same. The -EINVAL on lower == NULL || rtctime == NULL in rdtime/settime closes the RTC_RD_TIME/RTC_SET_TIME NULL-ioctl path.

Nice PR — the RTC fix on its own is worth merging, and the write-up makes each change easy to follow.

jun.pan added 2 commits December 2, 2026 10:06
RTC_RD_TIME / RTC_SET_TIME forward the caller-supplied ioctl argument
to the lower half without validation, and I2C_TRANSFER() passes dev and
msgs through unchecked.

- armino_rdtime() only had a DEBUGASSERT before calling gmtime_r(), and
  armino_settime() called mktime() directly, so
  ioctl(fd, RTC_RD_TIME, 0) writes through a NULL pointer.
- armino_i2c_transfer() dereferenced msgs and priv before any check,
  while the sibling ops setaddress() and setclock() already validate
  dev.

Return -EINVAL from the RTC ops and ERROR from armino_i2c_transfer()
when the arguments are NULL. Valid callers are unaffected.
- bk_pm: do not start the PM worker when rtos_init_queue() fails, back
  off 10 ms on a pop error, and keep the thread alive when alarm
  registration fails. Previously a NULL queue made the worker spin
  forever, and an alarm error returned from the thread body.
- RTC: bound the wait for the 32K tick reset in ana_rtc_hw_init() and
  skip it when s_ana_rtc_base_tick is 0. Both cases could hang the boot.
@Poly-J
Poly-J force-pushed the platform_fix_code_dev branch from d6d9ffb to b12a293 Compare December 2, 2026 02:06
@seokhun-eom24

Copy link
Copy Markdown
Contributor

🤖 AI Code Review · b12a293c339e

PR Summary: This PR adds defensive NULL checks to the I2C and RTC lowerhalf drivers, fixes a potential boot hang in the RTC driver by bounding an infinite loop, and improves the PM thread's robustness by cleaning up resources on init failure and preventing busy-spins or thread exits on transient errors. No new defects were introduced.

📋 Findings Overview (0 issues)

✅ Checked with no findings: Memory Leaks, Crashes, Hangs, Correctness

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants